﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Omu.ValueInjecter;

using Xiucai.BPM.Core;
using Xiucai.Common;
using Xiucai.Common.Data;
using Xiucai.Common.Data.Filter;
using Xiucai.BPM.Core.Bll;
using Xiucai.BPM.Core.Model;

namespace Xiucai.BPM.Admin.status_pic.ashx
{
    /// <summary>
    /// PIC 的摘要说明
    /// </summary>
    public class PIC : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var json = HttpContext.Current.Request["json"];
            var rpm = new RequestParamModel<CbdData>(context) { CurrentContext = context, Action = context.Request["Action"] };
            if (!string.IsNullOrEmpty(json))
            {
                rpm = JSONhelper.ConvertToObject<RequestParamModel<CbdData>>(json);
                rpm.CurrentContext = context;
            }
            int k = 0;
            switch (rpm.Action)
            {
                case "cbdNums":
                    //if (SysVisitor.Instance.CurrentUser.Mobile.Equals("888"))
                    //{
                    //    //dump all the things
                    //    context.Response.Write(DevConfigBll.Instance.CBDShortNames(0));
                    //}
                    //else
                    //{
                    int uid = PublicMethod.GetInt(rpm.Request("uid"));
                    if (uid == 0)
                        context.Response.Write(DevConfigBll.Instance.PICShortNames(SysVisitor.Instance.CurrentUser.KeyId));
                    else
                        context.Response.Write(DevConfigBll.Instance.PICShortNames(uid));

                    //}
                    break;

                case "detail":
                    var devID = rpm.Request("devID");
                    context.Response.Write(JsonDataForEasyUIdataGrid(rpm.Pageindex, rpm.Pagesize, rpm.Filter));
                    break;

                case "getUserRoleName":
                    string roleName = JsonDataForUserRoleName();
                    context.Response.Write(roleName);
                    break;

                case "getUserInfo":
                    string roleId = PublicMethod.GetString(rpm.Request("roleId"));
                    string userInfo = JsonDataForUserInfo(roleId);
                    context.Response.Write(userInfo);
                    break;

                case "add":
                    //string c = rpm.Request("sendcontent");
                    //string d = rpm.Request("cct");
                    string uinfo = rpm.Request("uinfo");
                    string cinfo = rpm.Request("cinfo");
                    int senduid = PublicMethod.GetInt(rpm.Request("senduid"));

                    EquipmentRepair equ = new EquipmentRepair();
                    equ.equType = "CBD";//报修型号为 测报灯
                    equ.repairContent = cinfo;//报修内容
                    equ.repairTime = DateTime.Now;//报修时间
                    equ.repairUserIds = uinfo;//售后服务人员ID列表
                    equ.userId = senduid;//报修人
                    equ.isRead = 0;//未读

                    int re = EquipmentRepairBll.Instance.AddEquipMentRepair(equ);
                    if (re > 0)
                    {
                        context.Response.Write(new JsonMessage { Success = true, Data = re.ToString(), Message = "ok" }.ToString());
                    }
                    else
                    {
                        context.Response.Write(new JsonMessage { Success = false, Data = re.ToString(), Message = "ok" }.ToString());
                    }

                    break;

                default:
                    break;


            }
        }

        private string JsonDataForUserRoleName()
        {
            return RoleBll.Instance.GetRoleName();
        }
        private string JsonDataForUserInfo(string roleId)
        {
            return RoleBll.Instance.GetUserInfoByRoleId(roleId);

        }

        private string JsonDataForEasyUIdataGrid(int pageindex, int pagesize, string filterJSON)
        {
            //数据权限条件
            string where = "";


            if (filterJSON != "")
            {
                where = FilterTranslator.ToSql(filterJSON) ;
            }

            var pcp = new ProcCustomPage("Vuppicsinfo")
            {
                KeyFields = "pid",
                PageIndex = pageindex,
                PageSize = pagesize,
                OrderFields = "ptime desc",
                WhereString = where
            };
            int recordCount;
            DataTable dt = DbUtils.GetPageWithSp(pcp, out recordCount);
            return JSONhelper.FormatJSONForEasyuiDataGrid(recordCount, dt);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}